import Vue from 'vue'
import VueRouter from 'vue-router'

import store from "../store"

function check(path,next){
  if(store.state.user.menus_url.find(item=>item==path)){
    next()
  }else{
    next("/home")
  }
}
Vue.use(VueRouter)
//导出二级路由
export const IndexRouters=[ {
  path:"caidan",
  component:()=>import("../pages/caidan/caidan.vue"),
  meta:{
    title:"菜单管理"
  },
  beforeEnter(to,from,next){
    check("/caidan",next)
  }
},
{
  path:"juese",
  component:()=>import("../pages/juese/juese.vue"),
  meta:{
    title:"角色管理"
  },
  beforeEnter(to,from,next){
    check("/juese",next)
  }
},
{
  path:"guanliyuan",
  component:()=>import("../pages/guanliyuan/guanliyuan.vue"),
  meta:{
    title:"管理员管理"
  },
  beforeEnter(to,from,next){
    check("/guanliyuan",next)
  }
},
{
  path:"shop-fenlei",
  component:()=>import("../pages/shop-fenlei/shop-fenlei.vue"),
  meta:{
    title:"商品分类"
  },
  beforeEnter(to,from,next){
    check("/shop-fenlei",next)
  }
},
{
  path:"shop-banner",
  component:()=>import("../pages/shop-banner/shop-banner.vue"),
  meta:{
    title:"轮播图管理"
  },
  beforeEnter(to,from,next){
    check("/shop-banner",next)
  }
},
{
  path:"shop-guige",
  component:()=>import("../pages/shop-guige/shop-guige.vue"),
  meta:{
    title:"商品规格"
  },
  beforeEnter(to,from,next){
    check("/shop-guige",next)
  }
},
{
  path:"shop-vip",
  component:()=>import("../pages/shop-vip/shop-vip.vue"),
  meta:{
    title:"会员管理"
  },
  // beforeEnter(to,from,next){
  //   check("/shop-vip",next)
  // }
},
{
  path:"shop-miaosha",
  component:()=>import("../pages/shop-miaosha/shop-miaosha.vue"),
  meta:{
    title:"秒杀活动"
  },
  beforeEnter(to,from,next){
    check("/shop-miaosha",next)
  }
},
{
  path:"shop-guanli",
  component:()=>import("../pages/shop-guanli/shop-guanli.vue"),
  meta:{
    title:"商品管理"
  },
  beforeEnter(to,from,next){
    check("/shop-guanli",next)
  }
},]
const routes = [
  {
    path:"/login",
    component:()=>import("../pages/login/login.vue")
  },
  {
    path:"/",
    component:()=>import("../pages/index/index.vue"),
    children:[
      {
        path:"home",
        component:()=>import("../pages/home/home.vue")
      },
     ...IndexRouters,
      {
        path:"",
        redirect: "home"
      }
    ]
  },
]

const router = new VueRouter({
  routes
})


//登陆拦截
router.beforeEach((to,from,next)=>{
  if(to.path=="/login"){
    next()
    return
  }
  if(store.state.user.token){
    next()
    return
  }
  next("/login")
})

export default router
